//
//  ZYNetworkTools.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/6/26.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit
import AFNetworking

//请求方式的枚举
enum MethodType: String {
    
    case GET = "GET"
    case POST = "POST"

}

//给请求成功或失败的闭包格式起一个别名
typealias SuccessClosure = (response: AnyObject?)->()
typealias FailureClosure = (error:NSError?)->()


class ZYNetworkTools: AFHTTPSessionManager {
    
    
    //全局访问点:
    
    static let sharedNetworkTools: ZYNetworkTools = {
        
        let tools = ZYNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
        
    }()
    
    
    
    //请求的参数:方式, url, 参数, 成功,失败
    func request(method: MethodType, urlString: String, parameters: AnyObject?, success: (response:AnyObject?)->(), failure:(error:NSError)->()){
    
        //如果是get请求的话
        if method == .GET {
            self.GET(urlString, parameters: parameters, progress: nil, success: { (_, resp) -> Void in
                
                success(response: resp)
                
                }, failure: { (_, error) -> Void in
                    failure(error: error)
            })
        
        } else {
        
        //如果是post请求的话
            self.POST(urlString, parameters: parameters, progress: nil, success: { (_, resp) -> Void in
                
                success(response: resp)
                
                }, failure: { (_, error) -> Void in
                    failure(error: error)
            })
        
        }
    
    }
    
}


extension ZYNetworkTools {

    func oauthLoadUserAccount(code:String, success: (response:AnyObject?)->(), failure:(error:NSError)->()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id":APPKEY,
            "client_secret":APPSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":REDIRECT_URI
        ]
        
        self.request(MethodType.POST, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
    // 请求个人信息数据  ===========
    
    func oauthLoadUserInfo(userAccont: ZYUserAccountModel, success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
    
        // 请求url
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":userAccont.access_token!,
            "uid":userAccont.uid!
            
            
            
        ]
        
        self.request(MethodType.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }

}

//=====================627新增请求数据的工具类
//请求首页的数据
extension ZYNetworkTools {
    
    func homeLoadStatusData(sinceId:Int64, maxId:Int64, success:SuccessClosure, failure:FailureClosure) {
        //请求的url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //请求的参数
        let params = ["access_token": ZYUserAccountTools.sharedTools.accessToken!,
            
            //直接用的话会类型不匹配
            //"since_id":sinceId,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        
        
        ]
        
        //发送请求
        request(.GET, urlString: urlString, parameters: params, success: success, failure: failure)
     
    }
    
}


//发布微博的界面
extension ZYNetworkTools {
    
    //发送文字微博
    func composeupdate(status: String , success: SuccessClosure, failure:FailureClosure) {
    
        // 请求url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        // 请求参数
        let params = [
            "access_token": ZYUserAccountTools.sharedTools.accessToken!,
            "status": status
            
        ]
        
        request(.POST, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
    //7.4 发送带有图片的文字
    func composeUpload(status: String, imageList:[UIImage], success:(response: AnyObject?)->(), failure:(error:NSError)->()) {
        
        //请求url
        let URLString = "https://upload.api.weibo.com/2/statuses/upload.json"
        //请求的参数
        let params = [
            "access_token": ZYUserAccountTools.sharedTools.accessToken!,
            "status":status
            
        ]
        
        POST(URLString, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
            
            for image in imageList {
                //image转二进制
                let data = UIImagePNGRepresentation(image)!
                formdata.appendPartWithFileData(data, name: "pic", fileName: "zhu", mimeType: "application/octet-stream")
                
            }
            
            }, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)
                
            }) { (_, error) -> Void in
                
                failure(error: error)
        }
        
    }
    
}















